package com.wuwangfu.transfor;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.operators.AbstractStreamOperator;
import org.apache.flink.streaming.api.operators.OneInputStreamOperator;
import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
import org.apache.flink.util.Collector;

/**
 * @Author jcshen
 * @Date 2023-02-22
 * @PackageName:com.wuwangfu.transfor
 * @ClassName: FlatMapFun
 * @Description:
 * @Version 1.0.0
 *
 * 输入一行，输出零到多行
 *
 */
public class FlatMapTransf {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> line = env.socketTextStream("localhost", 8888);

        SingleOutputStreamOperator<String> flated = line.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String value, Collector<String> out) throws Exception {
                String[] words = value.split(" ");
                for (String word : words) {
                    if (!"error".equalsIgnoreCase(word)) {
                        out.collect(word);
                    }
                }
            }
        });
        //flated.print();

        SingleOutputStreamOperator<String> myFated = line
                .transform("my-flat-map", TypeInformation.of(String.class), new MyFlatMap());

        myFated.print();

        env.execute();
    }

    /**
     * 自定义flatMap处理逻辑
     *  flatMap->DataStream->transform
     *      transform("Flat Map", outputType, new StreamFlatMap<>(clean(flatMapper)));
     *          "Flat Map"->名称
     *          outputType->输出类型
     *          StreamFlatMap->
     *              public class StreamFlatMap<IN, OUT> extends AbstractUdfStreamOperator<OUT, FlatMapFunction<IN, OUT>> implements OneInputStreamOperator<IN, OUT>
     *                  OneInputStreamOperator->
     *                  AbstractUdfStreamOperator->
     *                      public abstract class AbstractUdfStreamOperator<OUT, F extends Function> extends AbstractStreamOperator<OUT> implements OutputTypeConfigurable<OUT>
     *                          AbstractStreamOperator
     *
     *
     *
     */
    private static class MyFlatMap extends AbstractStreamOperator<String> implements OneInputStreamOperator<String,String> {
        @Override
        public void processElement(StreamRecord<String> element) throws Exception {

            String line = element.getValue();
            String[] words = line.split(" ");
            for (String word : words){
                if (!"error".equalsIgnoreCase(word)){
                    output.collect(element.replace(word));
                }
            }
        }
    }
}
